package com.cailu.rabmq.sender.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cailu.common.constant.DelaySenderQueuesCommon;

/**
 * @author cailu
 * @filename DelaySenderQueueConfig
 * @description 延迟消费 在【队列上】
 * @date 2020/3/31 11:06
 */
@Configuration
public class DelaySenderQueuesConfig {


	/**
	 * 功能描述 delay_queue_per_queue_ttl：TTL配置在队列上的缓冲队列
	 * @return org.springframework.amqp.core.Queue
	 * @author cailu
	 * @date 2020/3/30 17:21
	 */
	@Bean
	Queue delayQueuePerQueueTTL() {
		return QueueBuilder.durable(DelaySenderQueuesCommon.DELAY_QUEUES_QUEUE_TTL_NAME)
				//DLX 这里声明当前队列绑定的死信交换机
				.withArgument("x-dead-letter-exchange", DelaySenderQueuesCommon.DELAY_QUEUES_SLOW_NAME)
				//DLK  dead letter携带的routing key 这里声明当前队列的死信路由key
				.withArgument("x-dead-letter-routing-key", DelaySenderQueuesCommon.DELAY_QUEUES_QUEUE_NAME)
				//TTL 设置队列的过期时间
				.withArgument("x-message-ttl", DelaySenderQueuesCommon.DELAY_QUEUES_EXPIRATION)
				.build();
	}


	/**
	 * 功能描述  配置DLX
	 * @param
	 * @return org.springframework.amqp.core.DirectExchange
	 * @author cailu
	 * @date 2020/3/31 15:08
	 */
	@Bean
	DirectExchange delayExchange() {
		return new DirectExchange(DelaySenderQueuesCommon.DELAY_QUEUES_SLOW_NAME);
	}

	/**
	 * 功能描述  创建 实际消费队列
	 * @param
	 * @return org.springframework.amqp.core.Queue
	 * @author cailu
	 * @date 2020/3/31 15:08
	 */
	@Bean
	Queue delayProcessQueue() {
		return QueueBuilder.durable(DelaySenderQueuesCommon.DELAY_QUEUES_QUEUE_NAME)
				.build();
	}

	/**
	 * 功能描述  绑定
	 * @param delayProcessQueue 延迟处理队列
	 * @param delayExchange     延迟交换
	 * @return org.springframework.amqp.core.Binding
	 * @author cailu
	 * @date 2020/3/31 15:09
	 */
	@Bean
	Binding dlxBinding(Queue delayProcessQueue, DirectExchange delayExchange) {
		return BindingBuilder.bind(delayProcessQueue)
				.to(delayExchange)
				.with(DelaySenderQueuesCommon.DELAY_QUEUES_QUEUE_NAME);
	}
}
